package com.artconcurrency.chapter4;

/**
 * @ClassName com.artconcurrency.chapter4
 * Description: <类功能描述>. <br>
 * <p>
 * <使用说明>
 * </p>
 * @Author tree
 * @Date 2019-05-02 00:17
 * @Version 1.0
 */
public class Profile {
    /**
     *   第一次get()方法调用时会进行初始化（如果set方法没有调用），每个线程会调用一次
      */
    private static final ThreadLocal<Long> TIME_THREADLOCAL = new ThreadLocal<Long>() {
        @Override
        protected Long initialValue() {
            return System.currentTimeMillis();
        }
    };

    public static final void begin() {
        TIME_THREADLOCAL.set(System.currentTimeMillis());
    }

    public static final long end() {
        return System.currentTimeMillis() - TIME_THREADLOCAL.get();
    }

    public static void main(String[] args) throws Exception {
        Profile.begin();
        SleepUtils.second(1);
        System.out.println("Cost: " + Profile.end() + " mills");
    }
}